package com.example.dell.fastshopping.order;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.Trade.TradeRechargeActivity;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.order.model.OderInfoViewMolder;
import com.example.dell.fastshopping.user.address.AddressManagerActivity;
import com.example.dell.fastshopping.user.address.MyAddress;
import com.example.dell.fastshopping.utils.GlideUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${YSH} on 2019/4/24.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class OrderInfoActivity extends AppCompatActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.lc)
    ImageView lc;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_phone)
    TextView userPhone;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.has_location)
    RelativeLayout hasLocation;
    @BindView(R.id.choice_location)
    LinearLayout choiceLocation;
    @BindView(R.id.product_icon)
    ImageView productIcon;
    @BindView(R.id.oder_name)
    TextView oderName;
    @BindView(R.id.total_money)
    TextView totalMoney;
    @BindView(R.id.beizhu)
    EditText beizhu;
    @BindView(R.id.see_logistical)
    TextView seeLogistical;
    @BindView(R.id.pay)
    TextView pay;
    @BindView(R.id.item_order_info)
    LinearLayout itemOrderInfo;
    private OderInfoViewMolder molder;
    private int id;
    private OrderInfo mOrderInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        molder = ViewModelProviders.of(this).get(OderInfoViewMolder.class);
        title.setText("商品详情");
        Intent intent = getIntent();
        id = intent.getIntExtra("id",-1);
        getInfo();
    }

    private void getInfo() {
        molder.getOrderDetial(id).observe(this, new Observer<OrderInfo>() {
            @Override
            public void onChanged(@Nullable OrderInfo orderInfo) {
                mOrderInfo = orderInfo;
                if (orderInfo.getOrderAddress() == null) {
                    hasLocation.setVisibility(View.GONE);
                    choiceLocation.setVisibility(View.VISIBLE);
                } else {
                    hasLocation.setVisibility(View.VISIBLE);
                    choiceLocation.setVisibility(View.GONE);
                    userName.setText(orderInfo.getUserName());
                    userPhone.setText(orderInfo.getPhone());
                    address.setText(orderInfo.getOrderAddress());
                }

                if (orderInfo.getStatus() == 2) {
                    pay.setVisibility(View.VISIBLE);
                } else {
                    pay.setVisibility(View.GONE);
                    beizhu.setVisibility(View.GONE);
                }
                GlideUtils.setView(OrderInfoActivity.this, orderInfo.getPicString(), productIcon);
                oderName.setText(orderInfo.getProductName());
                totalMoney.setText(orderInfo.getTotalPriceString());
            }
        });
    }

    @OnClick({R.id.back, R.id.see_logistical, R.id.has_location, R.id.choice_location, R.id.pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.see_logistical:
                toActivity(LogisticalActivity.class);
                break;
            case R.id.choice_location:
                toActivityResult(AddressManagerActivity.class);
                break;
            case R.id.has_location:
                toActivityResult(AddressManagerActivity.class);
                break;
            case R.id.pay:
                toPayPage();
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void toPayPage() {
        Intent intent = new Intent(this, TradeRechargeActivity.class);
        intent.putExtra("from", "order");
        intent.putExtra("orderInfo", mOrderInfo);
        startActivityForResult(intent, 104);
    }

    private void toActivityResult(Class activityClass) {
        if (mOrderInfo.getStatus() != 2) {
            return;
        }
        Intent intent = new Intent(this, activityClass);
        intent.putExtra("from", "order");
        startActivityForResult(intent, 100);
    }

    private void toActivity(Class logisticalActivityClass) {
        Intent intent = new Intent(this, logisticalActivityClass);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            updateAddress(data);
        }
        if (requestCode == 104 && resultCode == RESULT_OK) {
            getInfo();
        }
    }


    private void updateAddress(Intent data) {
        MyAddress entity = (MyAddress) data.getSerializableExtra("entity");
        final String phone = entity.getPhone();
        final String address_ = entity.getArea() + entity.getAddress();
        final String userName = entity.getUserName();

        //TODO:后台不支持更新收货人
        molder.updateAddress(address_, id, phone).observe(this, new Observer<BaseResponse>() {
            @Override
            public void onChanged(@Nullable BaseResponse baseResponse) {

                if (!baseResponse.isSuccess()) {
                    return;
                }
                OrderInfoActivity.this.userName.setText(userName);
                userPhone.setText(phone);
                OrderInfoActivity.this.address.setText(address_);
                hasLocation.setVisibility(View.VISIBLE);
                choiceLocation.setVisibility(View.GONE);

            }
        });
    }


}
